// pages/tools/ertong/byjladd.js
var _$ = require("../../../../common/wxjs/wx_ajax");
const geturl = "et_yejl_cx";
const posturl = "et_yejl_tian_edit";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        nianling: '',
        uesrtoken:null,
        formdata: {
            "wyfs": 0,
            "pfhr": 0
        }
    },
    btnselect: function(e) {
        var that = this;
        that.setData({
            "formdata.wyfs": e.target.dataset.wyfs,
            "formdata.pfhr": e.target.dataset.pfhr,
        })
    },
    formSubmit: function(e) {
        var that = this;
        var userdata = e.detail.value;
        userdata = JSON.stringify(userdata);
        var litToken = that.data.uesrtoken;
        var tian = that.data.nianling;
        var methods = "POST";
        var header = "application/x-www-form-urlencoded";
        var dataobj = { nianling, userdata,litToken };
        wx.showToast({
            title: '正在提交',
            icon: 'loading',
            duration: 2000
        })
        _$.sendRrquest(posturl, methods, dataobj, header)
            .then(function(response) {
              console.log(response)
                if (response.data.status == 200 ) {
                    wx.showToast({
                        title: '提交成功',
                        icon: 'success',
                        duration: 2000
                    });
                    setTimeout(function() {
                        wx.hideToast()
                    }, 2000)
                } else {
                    wx.showToast({
                        title: '数据未修改',
                        icon: 'loading',
                        duration: 2000
                    });
                    setTimeout(function() {
                        wx.hideToast()
                    }, 2000)
                }
            }, function(error) {
                wx.showToast({
                    title: '提交失败',
                    duration: 2000
                });
                setTimeout(function() {
                    wx.hideToast()
                }, 2000)
                console.log(error);
            });
    },
    // 请求数据
    getdata: function(val) {
        var that = this;
        var litToken = val;
        var nianling = that.data.nianling
        var dataobj = { litToken, nianling};
        var methods = "GET";
        var header = "application/json";
        wx.showLoading({
            title: '加载中',
        })
        _$.sendRrquest(geturl, methods, dataobj, header)
            .then(function(response) {
                setTimeout(function() {
                    wx.hideLoading()
                }, 1000)
                console.log(response)
                if (!response.data.data.res==false){
                  that.setData({
                    formdata: response.data.data.res
                  });
                }
               
            }, function(error) {
                setTimeout(function() {
                    wx.hideLoading()
                }, 2000)
                wx.showToast({
                    title: '网络错误，请稍后再试',
                    duration: 2000
                })
                setTimeout(function() {
                    wx.hideToast()
                }, 2000)
                console.log(error);
            });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.nianling != undefined) {
            this.setData({
                nianling: options.nianling
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
              var that = this
      wx.getStorage({
        key: 'token',
        success: function (res) {
          that.setData({
            uesrtoken: res.data
          })
          that.getdata(res.data)
        }
      });
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})